﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient; 

namespace CinemaSystem
{
    class DBHelper
    {
        //1.连接字符串
        public static string connstr = "server=.;database=Cinema-Ticketing-System;integrated Security=true";
        //2.增删改
        public static int ExecuteNonQuery(string sqlstr)
        {
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand(sqlstr, conn);

            int i = 0;
            try
            {
                conn.Open();
                i = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return i;
        }

        //3.查询
        public static DataTable GetDataTable(string sqlstr)
        {
            SqlConnection conn = new SqlConnection(connstr);

            SqlCommand cmd = new SqlCommand(sqlstr, conn);

            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            try
            {
                sda.Fill(dt);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

            return dt;
        }
    
 }
}
